﻿using System;
using System.Text;
using System.Windows.Forms;
using Midapex.Net.Ftp;
using System.Threading;

namespace DukeyFtpServer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private FtpServer server = new FtpServer();
        private void btnStartService_Click(object sender, EventArgs e)
        {
            server.OnStartableStatueChange += new EventHandler<Midapex.TEventArgs<Midapex.IStartable>>(server_OnStartableStatueChange);
            try
            {
                server.Start();
                CheckServerStatus();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        void server_OnStartableStatueChange(object sender, Midapex.TEventArgs<Midapex.IStartable> e)
        {

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //NetDebuger.PrintErrorMessage("UNHANDLED ERROR:" + e.ExceptionObject.ToString());
        }
        Thread th = null;
        private void MainForm_Load(object sender, EventArgs e)
        {
            //using (server)
            //{
                //Console.WriteLine("测试帐号:anonymous(匿名登陆，具有读权限), ftp(密码:ftp,具有读写权限)");

                /*
                 * 服务器的最大连接数
                 */
                int maxconnect = 100;
              
                
                int.TryParse(txtLimitConnect.Text.Trim(), out maxconnect);
                server.Capacity = maxconnect;

                /*
                 * 连接超时时间
                 */
                int timeout = 2000000;
                int.TryParse(txtTimeOut.Text.Trim(), out timeout);
                server.HeartBeatPeriod = timeout * 60;  //120秒
                

                /*
                 * 创建一个使用FTP的用户，
                 */
                //XmlNodeList nodelist = XMLHelper.GetXmlNodeListByXpath("config.xml", "config/user");
                //foreach (XmlNode item in nodelist)
                //{
                //    string username = item.SelectSingleNode("username").InnerText;
                //    FtpUser user = new FtpUser(username);
                //    user.AllowRead = true;
                //    user.AllowWrite = (item.SelectSingleNode("allowWrite").InnerText == "0" ? false : true);
                //    user.HomeDir = item.SelectSingleNode("dir").InnerText;
                //    user.Password = item.SelectSingleNode("pwd").InnerText;
                //    user.MaxConnectionCount = 2;
                //    server.AddUser(user);
                //}




                FtpUser user = new FtpUser("admin");
                user.Password = "admin";
                user.AllowRead = true;
                user.AllowWrite = chkUpload.Checked;
               // user.HomeDir = Environment.CurrentDirectory;
                ///*
                // * 限制该帐号的用户的连接服务器的最大连接数
                // * 也就是限制该使用该帐号的FTP同时连接服务器的数量。
                // */
                //user.MaxConnectionCount = 2;
                user.HomeDir = "d:\\";
                ///*
                // * 限制用户的最大上传文件为20M，超过这个值上传文件会失败。
                // * 默认不限制该值，可以传输大文件。
                // */
                user.MaxUploadFileLength = 1024 * 1024 * 20;
                server.AddUser(user);
                
                ////把当前目录作为匿名用户的目录，测试目的(必须指定)
                server.AnonymousUser.HomeDir = Environment.CurrentDirectory;
                
                //Console.WriteLine("Press enter to exit...");
                //Console.ReadLine();
                //server.Stop();
                //while (1==1)
                //{
                //    if (server.IsRun==false)
                //    {
                //        server.Start();
                //    }
                //}
            //} 
            
            th = new Thread(new ThreadStart(run));
            th.Start();
            
        }
        private void run()
        {
            server.Start();         
            //th.Interrupt();
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            server.Stop();
            CheckServerStatus();
        }
        private void CheckServerStatus()
        {
            btnStartService.Enabled = !server.IsRun;
            btnStopService.Enabled = server.IsRun;

        }


    }
}
